package com.edu.bhu.admin.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


import com.edu.bhu.admin.domain.PreArea;
import com.edu.bhu.admin.mapper.PreAreaMapper;
import com.edu.bhu.common.core.domain.entity.SysUser;
import com.edu.bhu.common.core.text.Convert;
import com.edu.bhu.common.utils.DateUtils;
import com.edu.bhu.common.utils.ShiroUtils;
import com.edu.bhu.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.bhu.admin.mapper.PreRecordMapper;
import com.edu.bhu.admin.domain.PreRecord;
import com.edu.bhu.admin.service.IPreRecordService;

/**
 * 记录Service业务层处理
 *
 * @author yutianyang
 * @date 2022-04-06
 */
@Service
public class PreRecordServiceImpl implements IPreRecordService
{
    @Autowired
    private PreRecordMapper preRecordMapper;

    @Autowired
    private PreAreaMapper preAreaMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询记录
     *
     * @param recordId 记录主键
     * @return 记录
     */
    @Override
    public PreRecord selectPreRecordByRecordId(Long recordId)
    {
        return preRecordMapper.selectPreRecordByRecordId(recordId);
    }

    /**
     * 查询记录列表
     *
     * @param preRecord 记录
     * @return 记录
     */
    @Override
    public List<PreRecord> selectPreRecordList(PreRecord preRecord)
    {
        List<PreRecord> list = preRecordMapper.selectPreRecordList(preRecord);
        list = list.stream().map(m ->{
            PreArea preArea  = preAreaMapper.selectPreAreaByAreaId(m.getAreaId());
            m.setPreArea(preArea);
            SysUser sysUser = sysUserMapper.selectUserById(m.getUserId());
            m.setSysUser(sysUser);
            return m;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 查询某用户在十四天内的某状态的途径地记录数
     *
     * @param preRecord 核酸检测
     * @return 核酸检测集合
     */
    public Integer selectDangerCount(PreRecord preRecord){
        Date date = DateUtils.getDatePlusDays(DateUtils.getNowDate(), -14L);
        preRecord.setTime(date);
        return preRecordMapper.selectDangerCount(preRecord);
    }

    /**
     * 新增记录
     *
     * @param preRecord 记录
     * @return 结果
     */
    @Override
    public int insertPreRecord(PreRecord preRecord)
    {
        preRecord.setUserId(ShiroUtils.getSysUser().getUserId());
        return preRecordMapper.insertPreRecord(preRecord);
    }

    /**
     * 修改记录
     *
     * @param preRecord 记录
     * @return 结果
     */
    @Override
    public int updatePreRecord(PreRecord preRecord)
    {
        return preRecordMapper.updatePreRecord(preRecord);
    }

    /**
     * 批量删除记录
     *
     * @param recordIds 需要删除的记录主键
     * @return 结果
     */
    @Override
    public int deletePreRecordByRecordIds(String recordIds)
    {
        return preRecordMapper.deletePreRecordByRecordIds(Convert.toStrArray(recordIds));
    }

    /**
     * 删除记录信息
     *
     * @param recordId 记录主键
     * @return 结果
     */
    @Override
    public int deletePreRecordByRecordId(Long recordId)
    {
        return preRecordMapper.deletePreRecordByRecordId(recordId);
    }
}
